package com.qf.lanzhou.shop.api.controller;


import com.qf.lanzhou.shop.api.service.LoginpassService;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

/**
 * @Author jian    密码登录
 * @Date 2021/1/9 9:16
 * @JDK 1.8
 * @Version 1.0
 */
@Controller
@Api("密码登录")
public class LoginpassController extends BaseController {

    @Autowired
    private LoginpassService loginpassService;


    @ApiOperation(value = "用户密码登录接口")
    @PostMapping(value = "/loginpass")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名",
                    required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "用户密码",
                    required = true, dataType = "String"),
    })
    public BaseRespVO Userlogin(String userName, String password, HttpServletRequest req) {
        //根据用户名查询
        User user = loginpassService.selectUser(userName);
        if (user == null) {
            return convertErrorResult("用户不存在,请注册");
        }
        if (!user.getPassword().equals(password)) {
            return convertErrorResult("密码不正确,请重新输入");
        }
        int count = (int) req.getServletContext().getAttribute("count");
        count=count+1;
        req.getServletContext().setAttribute("count",count);
        req.getSession().setAttribute("user", user);


        //登录成功后添加登录时间,如果这个用户之前有登录过,就修改时间
        User user1 = (User) req.getSession().getAttribute("user");
        String userName1 = user1.getUserName();
        User loginTime=loginpassService.selectlogintime(userName1);
       if(loginTime==null){
          return convertErrorResult("用户不存在");
       }else {
          int k=loginpassService.update(userName1);
          if(k==1){
             User user2=loginpassService.selectallagain(userName1);
             req.getSession().setAttribute("user",user2);
          }
           return convertSuccessResult("100");
       }

    }

    @ApiOperation(value = "退出登录接口")
    @GetMapping(value = "/loginpassout")
    @ResponseBody
    public BaseRespVO loginpassout(HttpServletRequest req) {
        Object user = req.getSession().getAttribute("user");
        if(user==null){
            return convertErrorResult("你还未登录");
        }
        req.getSession().removeAttribute("user");
        int count = (int) req.getServletContext().getAttribute("count");
        count=count-1;
        req.getServletContext().setAttribute("count",count);
        return convertSuccessResult(100);
    }


    @ApiOperation(value = "判断session是否有uesr接口")
    @GetMapping(value = "/checkuser")
    @ResponseBody
    public BaseRespVO checkuser(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        if(user!=null){
            return convertErrorResult("你已登录");
        }
        return convertSuccessResult(100);
    }


  @ApiOperation(value = "首页显示登录用户的名字接口")
    @GetMapping(value = "/showname")
    @ResponseBody
    public BaseRespVO showuser(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        String userName = user.getUserName();
        return convertSuccessResult(userName);
    }


    @ApiOperation(value = "首页显示登录用户的登录时间接口")
    @GetMapping(value = "/showtime")
    @ResponseBody
    public BaseRespVO showlogin(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        System.out.println(user);
        Timestamp logintime = user.getLogintime();
        System.out.println(logintime);
        System.out.println("hhhh");
        return convertSuccessResult(logintime);
    }



    @ApiOperation(value = "修改登录密码接口")
    @PostMapping(value = "/updatepassword")
    @ResponseBody
    public BaseRespVO update(String userName,String password) {
        User user=loginpassService.select(userName);
        if(user==null){
            return convertErrorResult("用户名不存在");
        }else {
           int i=loginpassService.updatepassword(userName,password);
           if(i==1){
               return convertSuccessResult(user);
           }
        }
        return convertSuccessResult(user);
    }




}
